<?php
namespace app\api\service;
use think\Controller;
use think\Db;

class Austin extends Controller {
    static protected $err_code = 0;

    // 获取售后申请页面数据
    static public function get_order_server_austin_index_info($param) {
        if(!isset($param['member_id']) || !isset($param['product_id'])){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }
        try {
            $product = db('order_product_server')->alias('op')
                ->field('op.*, o.repair_id')->join('mob_order_server o', 'o.id = op.order_id')
                ->where('op.id', $param['product_id'])
                ->where('o.member_id', $param['member_id'])
                ->find();
            return ['code' => 200, 'data' => ['product' => $product], 'msg' =>'success'];
        }catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }
    
    // 获取售后单详情
    static public function get_austin_info($param) {
        if(!isset($param['austin_id'])){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }
        
        try {
            $data = db('order_austin')->where('id', $param['austin_id'])->find();
            if ($data['type'] == 2) {
                $data['order_sn'] = db('order_server')->where('id', $data['order_id'])->value('order_sn');
            }
            return ['code' => 200, 'data' => ['data' => $data], 'msg' =>'success'];
        }catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 撤销申请
    static public function revocation_austin($param) {
        if(!isset($param['austin_id']) || !isset($param['member_id'])){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }
        Db::startTrans();
        try {
            $rs = Db::table('mob_order_austin')->where('id', $param['austin_id'])->where('member_id', $param['member_id'])->update(['status' => 3]);
            if (!$rs) { // 售后单不存在
                self::$err_code = 400141;
                throw new \Exception('FAULT');
            }
            // 撤销之后检查订单是否还有售后  没有的话 需要把订单的售后状态改为0
            $austin = Db::table('mob_order_austin')->where('id', $param['austin_id'])->find();

            // 首先把对应的订单产品状态修改为0
            $models = ['', 'order_product_buy', 'order_product_server'];
            $model = $models[$austin['type']];
            Db::table('mob_' . $model)->where('id', $austin['product_id'])->update(['austin_status' => 0]);

            // 如果不是已取消和已关闭状态的售后数量为0  就将订单售后状态改为0
            $count = Db::table('mob_order_austin')->where('status', 'not in', '3,5')->where('order_id', $austin['order_id'])->count();
            if ($count == 0) {
                $models = ['', 'order_buy', 'order_server'];
                $model = $models[$austin['type']];
                Db::table('mob_' . $model)->where('id', $austin['order_id'])->update(['after_sale_status' => 0]);
            }

            Db::commit();
            return ['code' => 200, 'msg' => 'success'];
        }catch (\Exception $e) {
            Db::rollback();
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 申诉页面
    static public function get_complaint_page_info($param) {
        if(!isset($param['austin_id']) || !isset($param['member_id'])){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }
        try {
            $data = db('order_austin')->where('id', $param['austin_id'])->where('member_id', $param['member_id'])->find();
            $models = ['', 'order_buy', 'order_server'];
            $model = $models[$data['type']];
            $order = db($model)->field('order_sn')->where('id', $data['order_id'])->find();
            return ['code' => 200, 'data'=> ['data' => $data, 'order' => $order], 'msg' => 'success'];
        }catch (\Exception $e) {
            Db::rollback();
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 提交申诉
    static public function save_complaint($param) {
        if(!isset($param['austin_id']) || !isset($param['audit_result_msg']) || !isset($param['audit_result_cause'])){
            return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];
        }

        try {
            $data = [
                'audit_result_msg' => $param['audit_result_msg'],
                'audit_result_cause' => $param['audit_result_cause'],
                'status' => 4,
                'audit_result_time' => time()
            ];
            db('order_austin')->where('id', $param['austin_id'])->update($data);
            return ['code' => 200, 'msg' => 'success'];
        } catch (\Exception $e) {
            return ['code' => 0, 'msg' => $e->getMessage()];
        }
    }
}
